var pnum = "";   
var formula_version_id = $.trim($("#formula_version_id").val());
   var initData = function(num){
	    var param = {
	    	formula_version_id:formula_version_id,
			page_num:num,
			page_size:"10"
	    }
		$.ajax({
	        url: base + "/dataCenter/businessdata/findFormulaInfoList",
	        data: param,
	        success: function (data, textStatus, jqXHR) {
	        	if(data != null){
		        	var pageCount = data.pager.pageCount;
		        	var dataObj ={
		        			"list":data.list,
		         			"dataTool":{
		         				"title":{
		          			      	"constant_code": "常量"
		          			      	,"variable_code": "变量"
		          			      	,"formula_code":"公式编号"
		          			      	,"formula_name":"公式名称"
		          			      	,"expression":"表达式"
		          			      	,"orderBy":"排序规则"
		          			      	,"description":"描述"
		          			      	,"create_time":"创建时间"
		          			      	,"update_time":"修改时间"
		          			      	,"operation":"操作"
		            			},
		            			"titleCheckbox":false,
								"btn":[{"event":"updateObj","name":"编辑"},
								       {"event":"delObj","name":"删除"}]
							}
			  		};
		        	showPageTable('view', dataObj,num,pageCount,"initData");
	        	}

	        },
	        error: function () {
	        }
	    });
}
   
   var go_back = function(){
	   var url = base + "/dataCenter/formulaLibrary?name=设计公式库";
	   $.get(url, function(result){
		   $("#content").html("");
		   $("#content").html(result);
	   });
   }
   
   //编辑公式集信息
   var updateObj = function(obj){
	   var contentDIV = function(){
		   var content;
		   var data = {
				   "title":{
					   "id":"主键",
					   "constant_code":"指定常量",
					   "variable_code":"指定变量",
					   "formula_code":"公式编号",
					   "formula_name":"公式全称",
					   "expression":"指定表达式",
					   "description":"描述"
				   },"text":{
					   "id":obj.id,
					   "constant_code":obj.constant_code,
					   "variable_code":obj.variable_code,
					   "formula_code":obj.formula_code,
					   "formula_name":obj.formula_name,
					   "expression":obj.expression,
					   "description":obj.description
				   },"input":{
					   "id":"hidden",
					   "constant_code":"text",
					   "variable_code":"text",
					   "formula_code":"text",
					   "formula_name":"text",
					   "expression":"text",
					   "description":"text"
				   },"input_init":{
				   }
		   };
		   var getDiv = dc_update.innerHTML;
		   laytpl(getDiv).render(data,function(html){
			   content = html;
		   });
		   return content;
	   }
	   layopen(contentDIV());
   }
   
   //新增公式集
   var addFormulaData = function(){
	   var contentDIV = function(){
		   var content;
		   var data = {
				   "title":{
					   "constant_code":"指定常量",
					   "variable_code":"指定变量",
					   "formula_code":"公式编号",
					   "formula_name":"公式全称",
					   "expression":"指定表达式",
					   "description":"描述"
				   },"text":{
					   "constant_code":"",
					   "variable_code":"",
					   "formula_code":"",
					   "formula_name":"",
					   "expression":"",
					   "description":""
				   },"input":{
					   "constant_code":"text",
					   "variable_code":"text",
					   "formula_code":"text",
					   "formula_name":"text",
					   "expression":"text",
					   "description":"text"
				   },"input_init":{
				   }
		   };
		   var getDiv = dc_update.innerHTML;
		   laytpl(getDiv).render(data,function(html){
			   content = html;
		   });
		   return content;
	   }
	   layopen(contentDIV());
   }
   
   //验证数据
   var verificationData = function(data){
		if(data.constant_code == ""){
			var obj = $("input[name='constant_code']");
			obj.focus();
			layer.tips('请填写此字段!', obj);
			return false;
		}
		if(data.variable_code == ""){
			var obj = $("input[name='variable_code']");
			obj.focus();
			layer.tips('请填写此字段!', obj);
			return false;
		}
		if(data.formula_code == ""){
			var obj = $("input[name='formula_code']");
			obj.focus();
			layer.tips('请填写此字段!', obj);
			return false;
		}
		if(data.formula_name == ""){
			var obj = $("input[name='formula_name']");
			obj.focus();
			layer.tips('请填写此字段!', obj);
			return false;
		}
		if(data.expression == ""){
			var obj = $("input[name='expression']");
			obj.focus();
			layer.tips('请填写此字段!', obj);
			return false;
		}
		if(data.description == ""){
			var obj = $("input[name='description']");
			obj.focus();
			layer.tips('请填写此字段!', obj);
			return false;
		}
		return true;
   }
   
   //将数据渲染到弹窗
   var layopen = function(content){
		layer.open({
		  	title:'更新公式版本库'
			,content: content
	    	,area: ['800px', '600px']
		    ,scrollbar: false
		  	,btn: ['确认', '取消']
			,yes: function(index,layero){ //确认
 		  	    var data = $(layero).find("form").serializeJSON();
 		  	    if(!verificationData(data)){//验证失败!
 		  	    	return false;
 		  	    }
				$.post(base+"/dataCenter/businessdata/updateFormulaData",{
					formula_version_id:formula_version_id,
					id:data.id==null?"":data.id,
					constant_code:$.trim(data.constant_code),
					variable_code:$.trim(data.variable_code),
					formula_code:$.trim(data.formula_code),
					formula_name:$.trim(data.formula_name),
					expression:data.expression,
					description:data.description
				},
		  		function (data,status){
					if(data!=""&&data!=null){
						if(data.statusCode == 0){
							layer.msg('更新成功', {icon: 1,time:2000});
						}else{
							layer.msg(data.message, {icon: 2,time:2000});
						}
					}else{
						layer.msg('更新失败!', {icon: 2,time:2000});
					}
					initData(pnum);
		  		 })
			}
	});
 }
   
   //删除公式
   var delObj = function(obj){
	   layer.confirm('确认删除?',{btn : [ '确定', '取消' ]},function(index){
		   layer.close(index);
		   var param = {id:obj.id}
		   $.ajax({
			   url:base+"/dataCenter/businessdata/delFormulaDataById",
			   data:param,
			   success:function(data,status){
				    if(data.statusCode == 0){
						layer.msg(data.message, {
							icon : 1
						});
				    }else{
						layer.msg(data.message, {
							icon : 5
						});
				    }
				    initData(pnum);
			   }
		   });
	   });
   }
   
   $(document).ready(function(){
	   initData(1);
	   $("#addData").click(addFormulaData);
   });